package com.village.friend.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.village.friend.constant.MsgCodeEnum;
import com.village.friend.dto.request.UserBlackAddDto;
import com.village.friend.dto.request.UserBlackListDto;
import com.village.friend.dto.response.BaseResponse;
import com.village.friend.dto.response.LikeOrBlackUserRspDto;
import com.village.friend.entity.User;
import com.village.friend.entity.UserBlackList;
import com.village.friend.redis.RedisService;
import com.village.friend.service.UserBlackListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户黑名单表 前端控制器
 * </p>
 *
 * @author yl
 * @since 2021-09-02
 */
@RestController
@RequestMapping("/appUserBlackList")
@Api(tags = "用户黑名单模块")
public class UserBlackListController {

    @Autowired
    private UserBlackListService userBlackListService;

    @Autowired
    RedisService redisService;

    @ApiOperation(value = "添加或删除黑名单")
    @PostMapping(value = "/black")
    public BaseResponse<String> black(@RequestBody UserBlackAddDto param) {

        User userRedis = redisService.getUser(param.getToken());
        QueryWrapper<UserBlackList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("owner_id", userRedis.getUid());
        queryWrapper.eq("block_id", param.getUserIdBlack());
        if (param.getBlack() == 1) {
            UserBlackList one = userBlackListService.getOne(queryWrapper);
            if (one == null) {
                UserBlackList userBlackList = new UserBlackList();
                userBlackList.setOwnerId(userRedis.getUid());
                userBlackList.setBlockId(param.getUserIdBlack());
                userBlackListService.save(userBlackList);
            }
        } else if (param.getBlack() == 0) {

            userBlackListService.remove(queryWrapper);
        }
        return new BaseResponse(MsgCodeEnum.SUCCESS, null);
    }


    @ApiOperation(value = "分页查询")
    @GetMapping("/page")
    public BaseResponse<IPage<LikeOrBlackUserRspDto>> userPage(UserBlackListDto param) {
        if (param == null || param.getBeBlack() == null) {
            return new BaseResponse(MsgCodeEnum.PARAMS_ERROR, null);
        }
        User userRedis = redisService.getUser(param.getToken());
        // 查询用户信息
        IPage<LikeOrBlackUserRspDto> pageData = new Page<>(param.getPage(), param.getLimit());
        Integer limit = param.getLimit();
        Integer offset = (param.getPage() - 1) * limit;
        List<User> list;
        Integer total;
        if (param.getBeBlack() == 1) {
            list = userBlackListService.list(userRedis.getUid(), offset, limit);
            total = userBlackListService.listCount(userRedis.getUid());
        } else {
            list = userBlackListService.listBeBlack(userRedis.getUid(), offset, limit);
            total = userBlackListService.listCountBeBlack(userRedis.getUid());
        }
        List<LikeOrBlackUserRspDto> dataList = new ArrayList<>();
        for (User userEntity : list) {
            LikeOrBlackUserRspDto data = new LikeOrBlackUserRspDto();
            BeanUtils.copyProperties(userEntity, data, "userId");
            data.setUserId(userEntity.getUid());
            dataList.add(data);
        }
        pageData.setTotal(total);
        pageData.setRecords(dataList);
        return new BaseResponse(MsgCodeEnum.SUCCESS, pageData);
    }
}

